package idiet.view;

import idiet.Activator;
import idiet.bean.FoodMaterial;
import idiet.bean.FoodMaterialCategory;
import idiet.bean.Meal;
import idiet.bean.MealFamily;
import idiet.bean.MealRecipe;
import idiet.bean.MealTaste;
import idiet.bean.Unit;
import idiet.service.FoodMaterialCategoryService;
import idiet.service.FoodMaterialService;
import idiet.service.MealFamilyService;
import idiet.service.MealService;
import idiet.service.MealTasteService;
import idiet.service.UnitService;
import idiet.util.ViewUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ComboBoxViewerCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import swing2swt.layout.BorderLayout;

public class ViewPartMealAdd extends ViewPart{
	
	public static final String ID="iDiet.view.meal.add";
	private Text textName;
	private Text textAlias;
	private Text textFeature;
	private Text textProcedure;
	private Text textComment;
	private Text textEffect;
	private Text textPicture;
	private Table tableChosedMaterial;
	private Table tableMaterial;
	private Text textMaterial;
	private Image image;
	private Combo comboTaste;
	private Combo comboFamily;
	private Combo comboPrimaryCategory;
	private Combo comboSecondaryCategory;
	private Canvas canvasMealPicture;
	private CheckboxTableViewer checkboxTableViewer;
	private CheckboxTableViewer recipeCheckboxTableViewer;
	private List<FoodMaterial> materials=new ArrayList<FoodMaterial>();
	private List<MealRecipe> recipe=new ArrayList<MealRecipe>();
	private MealTasteService tasteServie=Activator.getApplicationContext().getBean(MealTasteService.class);
	private MealFamilyService mealFamilyService=Activator.getApplicationContext().getBean(MealFamilyService.class);
	private FoodMaterialCategoryService materialCategroyService=Activator.getApplicationContext().getBean(FoodMaterialCategoryService.class);
	private FoodMaterialService foodMaterialService=Activator.getApplicationContext().getBean(FoodMaterialService.class);
	private UnitService unitService=Activator.getApplicationContext().getBean(UnitService.class);
	private MealService mealService=Activator.getApplicationContext().getBean(MealService.class);
	private Button btnNewButtonSave;
	public ViewPartMealAdd() {
	}
	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(2, false));
		
		Group grpMealsInformation = new Group(parent, SWT.NONE);
		grpMealsInformation.setLayout(new GridLayout(3, false));
		GridData gd_grpMealsInformation = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 2);
		gd_grpMealsInformation.widthHint = 359;
		gd_grpMealsInformation.heightHint = 249;
		grpMealsInformation.setLayoutData(gd_grpMealsInformation);
		grpMealsInformation.setText("Meal's Information");
		
		Label lblNewLabel = new Label(grpMealsInformation, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setAlignment(SWT.RIGHT);
		lblNewLabel.setText("Name");
		
		textName = new Text(grpMealsInformation, SWT.BORDER);
		textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		Label lblNewLabel_1 = new Label(grpMealsInformation, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setAlignment(SWT.RIGHT);
		lblNewLabel_1.setText("Alias");
		
		textAlias = new Text(grpMealsInformation, SWT.BORDER);
		textAlias.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(grpMealsInformation, SWT.NONE);
		
		Label lblNewLabel_2 = new Label(grpMealsInformation, SWT.NONE);
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		lblNewLabel_2.setAlignment(SWT.RIGHT);
		lblNewLabel_2.setText("Feature");
		
		textFeature = new Text(grpMealsInformation, SWT.BORDER);
		GridData gd_textFeature = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_textFeature.heightHint = 81;
		textFeature.setLayoutData(gd_textFeature);
		
		Label lblPicture = new Label(grpMealsInformation, SWT.NONE);
		lblPicture.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		lblPicture.setAlignment(SWT.RIGHT);
		lblPicture.setText("Effect");
		
		textEffect = new Text(grpMealsInformation, SWT.BORDER);
		GridData gd_textEffect = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_textEffect.heightHint = 85;
		textEffect.setLayoutData(gd_textEffect);
		
		Label lblNewLabel_3 = new Label(grpMealsInformation, SWT.NONE);
		lblNewLabel_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_3.setAlignment(SWT.RIGHT);
		lblNewLabel_3.setText("Taste");
		
		comboTaste = new Combo(grpMealsInformation, SWT.NONE);
		comboTaste.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		List<MealTaste> tastes=tasteServie.getMealTastesByMealTasteName(null);
		for(MealTaste taste:tastes){
			comboTaste.add(taste.getName());
			comboTaste.setData(taste.getName(),taste.getId());
		}
		new Label(grpMealsInformation, SWT.NONE);
		
		Label lblFamily = new Label(grpMealsInformation, SWT.NONE);
		lblFamily.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblFamily.setAlignment(SWT.RIGHT);
		lblFamily.setText("Family");
		
		comboFamily = new Combo(grpMealsInformation, SWT.NONE);
		comboFamily.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		List<MealFamily> families=mealFamilyService.getMealFamilysByMealFamilyName(null);
		for(MealFamily family:families){
			comboFamily.add(family.getName());
			comboFamily.setData(family.getName(),family.getId());
		}
		new Label(grpMealsInformation, SWT.NONE);
		
		Label lblNewLabel_4 = new Label(grpMealsInformation, SWT.NONE);
		lblNewLabel_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_4.setText("Picture");
		
		textPicture = new Text(grpMealsInformation, SWT.BORDER);
		textPicture.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Button btnNewButton = new Button(grpMealsInformation, SWT.NONE);
		btnNewButton.setText("Browse");
		btnNewButton.addSelectionListener(new SelectionAdapter(){
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog dialog=new FileDialog(getSite().getShell(),SWT.OPEN);
				dialog.setFilterPath(System.getProperty("user.home"));
		        String[] filterExt = { "*.jpg;*.jpeg","*.jpeg", "*.doc", ".rtf", "*.*" };
		        dialog.setFilterExtensions(filterExt);
		     // Set the text
		        dialog.setText("Select File");
		        // Set filter on .txt files
		        dialog.setFilterExtensions(filterExt);
		        // Put in a readable name for the filter
		        //dialog.setFilterNames(new String[] { "Textfiles(*.txt)" });
		        // Open Dialog and save result of selection
		        String selectionFile = dialog.open();
		        if(selectionFile!=null){
		        	textPicture.setText(selectionFile);
		        	image = new Image(getSite().getShell().getDisplay(),selectionFile);
		        	canvasMealPicture.redraw();
		        }
		        System.out.println(selectionFile);
			}
		});
		Label lblProcedure = new Label(grpMealsInformation, SWT.NONE);
		lblProcedure.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		lblProcedure.setAlignment(SWT.RIGHT);
		lblProcedure.setText("Procedure");
		
		textProcedure = new Text(grpMealsInformation, SWT.BORDER);
		GridData gd_textProcedure = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_textProcedure.heightHint = 148;
		textProcedure.setLayoutData(gd_textProcedure);
		
		Label lblComment = new Label(grpMealsInformation, SWT.NONE);
		lblComment.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		lblComment.setAlignment(SWT.RIGHT);
		lblComment.setText("Comment");
		
		textComment = new Text(grpMealsInformation, SWT.BORDER);
		GridData gd_textComment = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_textComment.heightHint = 82;
		textComment.setLayoutData(gd_textComment);
		
		Group grpChooseFoodMaterial = new Group(parent, SWT.NONE);
		grpChooseFoodMaterial.setText("Choose Food Material");
		grpChooseFoodMaterial.setLayout(new GridLayout(3, false));
		GridData gd_grpChooseFoodMaterial = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_grpChooseFoodMaterial.widthHint = 485;
		grpChooseFoodMaterial.setLayoutData(gd_grpChooseFoodMaterial);
		
		Composite composite_2 = new Composite(grpChooseFoodMaterial, SWT.NONE);
		composite_2.setLayout(new GridLayout(7, false));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 3, 1));
		
		Label lblNewLabel_5 = new Label(composite_2, SWT.NONE);
		lblNewLabel_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_5.setText("Category");
		
		comboPrimaryCategory = new Combo(composite_2, SWT.NONE);
		comboPrimaryCategory.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		List<FoodMaterialCategory> primaryCategories=materialCategroyService.getParentFoodMaterialCategories();
		for(FoodMaterialCategory category:primaryCategories){
			comboPrimaryCategory.add(category.getName());
			comboPrimaryCategory.setData(category.getName(),category.getId());
		}
		comboPrimaryCategory.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				String selection=comboPrimaryCategory.getText();
				if(!selection.trim().equals("")){
					comboSecondaryCategory.setText("");
					
					FoodMaterialCategory parent=new FoodMaterialCategory();
					parent.setId(new Integer(comboPrimaryCategory.getData(selection).toString()));
					List<FoodMaterialCategory> secondaryCategories=materialCategroyService.getChildFoodMaterialCategories(parent);
					for(FoodMaterialCategory category:secondaryCategories){
						comboSecondaryCategory.add(category.getName());
						comboSecondaryCategory.setData(category.getName(),category.getId());
					}
					/*
					 * input checkbox table viewer
					 */
					materials=foodMaterialService.getFoodMaterials(parent, true);
					checkboxTableViewer.setInput(materials);
					//checkboxTableViewer.setAllGrayed(true);
					//checkboxTableViewer.setAllChecked(true);
					//checkboxTableViewer.refresh();
				}
			}
		});
		
		Label lblNewLabel_6 = new Label(composite_2, SWT.NONE);
		lblNewLabel_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_6.setText("Sub Category");
		
		comboSecondaryCategory = new Combo(composite_2, SWT.NONE);
		comboSecondaryCategory.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		comboSecondaryCategory.addSelectionListener(new SelectionAdapter(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				String selection=comboSecondaryCategory.getText();
				if(!selection.trim().equals("")){
					FoodMaterialCategory secondary=new FoodMaterialCategory();
					secondary.setId(new Integer(comboSecondaryCategory.getData(selection).toString()).intValue());
					materials=foodMaterialService.getFoodMaterials(secondary,false);
					checkboxTableViewer.setInput(materials);
					
				}
			}
		});
		
		Label lblNewLabel_7 = new Label(composite_2, SWT.NONE);
		lblNewLabel_7.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_7.setText("Material");
		
		textMaterial = new Text(composite_2, SWT.BORDER);
		textMaterial.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Button btnNewButtonQuery = new Button(composite_2, SWT.NONE);
		btnNewButtonQuery.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String primaryCategory=comboPrimaryCategory.getText().trim().equals("")?null:comboPrimaryCategory.getText().trim();
				String secondaryCategory=comboSecondaryCategory.getText().trim().equals("")?null:comboSecondaryCategory.getText().trim();
				String materialName=textMaterial.getText().trim().equals("")?null:textMaterial.getText().trim();
				if(primaryCategory!=null){
					FoodMaterialCategory category=new FoodMaterialCategory();
					if(secondaryCategory!=null){
						category.setId(new Integer(comboSecondaryCategory.getData(secondaryCategory).toString()));
						materials=foodMaterialService.getFoodMaterials(materialName,category, false);
					}else{
						category.setId(new Integer(comboPrimaryCategory.getData(primaryCategory).toString()));
						materials=foodMaterialService.getFoodMaterials(materialName,category, true);
					}
				}else{
					materials=foodMaterialService.getFoodMaterials(materialName);
				}
				checkboxTableViewer.setInput(materials);
				
			}
		});
		btnNewButtonQuery.setText("Query");
		
		Group group = new Group(grpChooseFoodMaterial, SWT.NONE);
		group.setLayout(new BorderLayout(0, 0));
		GridData gd_group = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_group.widthHint = 171;
		group.setLayoutData(gd_group);
		
		Composite composite_1 = new Composite(group, SWT.NONE);
		composite_1.setLayoutData(BorderLayout.CENTER);
		composite_1.setLayout(new BorderLayout(0, 0));
		
		checkboxTableViewer = CheckboxTableViewer.newCheckList(composite_1, SWT.BORDER | SWT.FULL_SELECTION);
		tableMaterial = checkboxTableViewer.getTable();
		tableMaterial.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				/*TableItem[] tableItems=tableMaterial.getSelection();
				TableItem tableItem=tableItems[0];
				tableItem.setGrayed(true);*/
				StructuredSelection selection =(StructuredSelection) checkboxTableViewer.getSelection();
				//int[] selectionIndex=checkboxTableViewer.getTable().getSelectionIndices();
				Object o=selection.getFirstElement();
				//if(!checkboxTableViewer.getChecked(o)){
				//	checkboxTableViewer.setChecked(o, true);
					FoodMaterial m=(FoodMaterial)o;
					MealRecipe recipeItem=new MealRecipe();
					recipeItem.setFoodMaterial(m);
					//recipeItem.setVersion(0);
					recipeItem.setQty(new BigDecimal(100));
					Unit unit=new Unit();
					unit.setId(1);
					recipeItem.setUnit(unit);
					recipe.add(recipeItem);
					//recipeCheckboxTableViewer.add(recipeItem);
					recipeCheckboxTableViewer.setInput(recipe);
					
					checkboxTableViewer.remove(o);
					recipeCheckboxTableViewer.refresh();
					
				//}
				//MessageDialog.openInformation(getSite().getShell(), "dddd",m.getName() );
			}
		});
		tableMaterial.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		tableMaterial.setLinesVisible(true);
		tableMaterial.setHeaderVisible(true);
		tableMaterial.setLayoutData(BorderLayout.CENTER);
		checkboxTableViewer.setContentProvider(new ArrayContentProvider());
		//checkboxTableViewer.setSorter(new ViewerSorter());
		checkboxTableViewer.setComparator(new ViewerComparator(){

			@Override
			public int category(Object element) {
				// TODO Auto-generated method stub
				return super.category(element);
			}

			@Override
			public int compare(Viewer viewer, Object e1, Object e2) {
				// TODO Auto-generated method stub
				FoodMaterial m1 = (FoodMaterial) e1;  
				FoodMaterial m2 = (FoodMaterial) e2;  
		        int rc = 0;  
		        rc=m1.getName().compareTo(m2.getName());
		        /*switch (propertyIndex) {  
		        case 0:  
		            rc = p1.getFirstName().compareTo(p2.getFirstName());  
		            break;  
		        case 1:  
		            rc = p1.getLastName().compareTo(p2.getLastName());  
		            break;  
		        default:  
		            rc = 0;  
		        } */ 
		        // If descending order, flip the direction  
		        if (tableMaterial.getSortDirection() == SWT.DOWN) {  
		            rc = -rc;  
		        }  
		        return rc; 
				//return super.compare(viewer, e1, e2);
			}

			@Override
			public boolean isSorterProperty(Object element, String property) {
				// TODO Auto-generated method stub
				return super.isSorterProperty(element, property);
			}

			@Override
			public void sort(Viewer viewer, Object[] elements) {
				// TODO Auto-generated method stub
				super.sort(viewer, elements);
			}
		});
		TableViewerColumn tableViewerColumn = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
		final TableColumn tblclmnNewColumn = tableViewerColumn.getColumn();
		tblclmnNewColumn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//tableViewerColumn.
				//e.
				tableMaterial.setSortColumn(tblclmnNewColumn);
				if(tableMaterial.getSortDirection()==SWT.UP)
					tableMaterial.setSortDirection(SWT.DOWN);
				else
					tableMaterial.setSortDirection(SWT.UP);
				//MessageDialog.openInformation(getSite().getShell(), "dddd","kkkkkkkkkkk");
			}
		});
		tblclmnNewColumn.setWidth(250);
		tblclmnNewColumn.setText("Food Material");
		tableViewerColumn.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((FoodMaterial)element).getName();
			}
			
		});
		
		Composite composite_3 = new Composite(grpChooseFoodMaterial, SWT.NONE);
		composite_3.setLayout(new GridLayout(1, false));
		composite_3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		new Label(composite_3, SWT.NONE);
		
		Button btnNewButtonSingle = new Button(composite_3, SWT.NONE);
		btnNewButtonSingle.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection selection =(StructuredSelection) checkboxTableViewer.getSelection();
				if(selection.isEmpty())
					return;
				//Object[]  selections =checkboxTableViewer.getCheckedElements();
				Object o=selection.getFirstElement();
				//if(selections.length>0){
					//Object o=selections[0];
					FoodMaterial m=(FoodMaterial)o;
					MealRecipe recipeItem=new MealRecipe();
					recipeItem.setFoodMaterial(m);
					recipeItem.setQty(new BigDecimal(100));
					Unit unit=new Unit();
					unit.setId(1);
					recipeItem.setUnit(unit);
					recipe.add(recipeItem);
					recipeCheckboxTableViewer.setInput(recipe);
					
					checkboxTableViewer.remove(o);
					recipeCheckboxTableViewer.refresh();
				//}
			}
		});
		btnNewButtonSingle.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		btnNewButtonSingle.setText(">");
		
		Button btnNewButtonMulti = new Button(composite_3, SWT.NONE);
		btnNewButtonMulti.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Object[]  selections =checkboxTableViewer.getCheckedElements();
				for(Object o:selections){
					//Object o=selections[0];
					FoodMaterial m=(FoodMaterial)o;
					MealRecipe recipeItem=new MealRecipe();
					recipeItem.setFoodMaterial(m);
					recipeItem.setQty(new BigDecimal(100));
					Unit unit=new Unit();
					unit.setId(1);
					recipeItem.setUnit(unit);
					recipe.add(recipeItem);
					checkboxTableViewer.remove(o);
				}
				recipeCheckboxTableViewer.setInput(recipe);
				recipeCheckboxTableViewer.refresh();
			}
		});
		btnNewButtonMulti.setText(">>");
		
		Button btnNewButtonMultiUnchose = new Button(composite_3, SWT.NONE);
		btnNewButtonMultiUnchose.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Object[] selections=recipeCheckboxTableViewer.getCheckedElements();
				for(Object o:selections){
					MealRecipe recipeItem=(MealRecipe)o;
					FoodMaterial m=recipeItem.getFoodMaterial();
					checkboxTableViewer.add(m);
					recipe.remove(recipeItem);
					recipeCheckboxTableViewer.remove(o);
				}
				recipeCheckboxTableViewer.refresh();
			}
		});
		btnNewButtonMultiUnchose.setText("<<");
		
		Button btnNewButtonSingleUnchose = new Button(composite_3, SWT.NONE);
		btnNewButtonSingleUnchose.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				StructuredSelection selection =(StructuredSelection) recipeCheckboxTableViewer.getSelection();
				if(selection.isEmpty())
					return;
				Object o=selection.getFirstElement();
				MealRecipe recipeItem=(MealRecipe)o;
				FoodMaterial m=recipeItem.getFoodMaterial();
				checkboxTableViewer.add(m);
				recipe.remove(recipeItem);
				recipeCheckboxTableViewer.remove(o);
				recipeCheckboxTableViewer.refresh();
			}
			
		});
		btnNewButtonSingleUnchose.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
		btnNewButtonSingleUnchose.setText("<");
		
		Group group_1 = new Group(grpChooseFoodMaterial, SWT.NONE);
		group_1.setLayout(new BorderLayout(0, 0));
		GridData gd_group_1 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_group_1.widthHint = 276;
		group_1.setLayoutData(gd_group_1);
		
		recipeCheckboxTableViewer = CheckboxTableViewer.newCheckList(group_1, SWT.BORDER | SWT.FULL_SELECTION);
		tableChosedMaterial = recipeCheckboxTableViewer.getTable();
		tableChosedMaterial.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				StructuredSelection selection =(StructuredSelection) recipeCheckboxTableViewer.getSelection();
				Object o=selection.getFirstElement();
				MealRecipe recipeItem=(MealRecipe)o;
				FoodMaterial m=recipeItem.getFoodMaterial();
				checkboxTableViewer.add(m);
				recipe.remove(recipeItem);
				recipeCheckboxTableViewer.remove(o);
			}
		});
		tableChosedMaterial.setHeaderVisible(true);
		tableChosedMaterial.setLinesVisible(true);
		recipeCheckboxTableViewer.setContentProvider(new ArrayContentProvider());
		
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(recipeCheckboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_1.getColumn();
		tblclmnNewColumn_1.setAlignment(SWT.CENTER);
		tblclmnNewColumn_1.setWidth(150);
		tblclmnNewColumn_1.setText("Material");
		tableViewerColumn_1.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				return ((MealRecipe)element).getFoodMaterial().getName();
			}
			
		});
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(recipeCheckboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_2 = tableViewerColumn_2.getColumn();
		tblclmnNewColumn_2.setWidth(100);
		tblclmnNewColumn_2.setText("Qty");
		tableViewerColumn_2.setEditingSupport(new EditingSupport(recipeCheckboxTableViewer){
			
			@Override
			protected CellEditor getCellEditor(Object element) {
				// TODO Auto-generated method stub
				return new TextCellEditor(recipeCheckboxTableViewer.getTable());
			}

			@Override
			protected boolean canEdit(Object element) {
				// TODO Auto-generated method stub
				return true;
			}

			@Override
			protected Object getValue(Object element) {
				// TODO Auto-generated method stub
				BigDecimal qty=((MealRecipe)element).getQty();
				return qty.toString();
			}

			@Override
			protected void setValue(Object element, Object value) {
				// TODO Auto-generated method stub
				System.out.println("set value "+value.toString());
				((MealRecipe)element).setQty(new BigDecimal(value.toString().equals("")?"0":value.toString()));
				//tableViewer.update(element, null);
				recipeCheckboxTableViewer.refresh();
			}
			
		});
		tableViewerColumn_2.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				BigDecimal qty=((MealRecipe)element).getQty();
				return qty==null?"":qty.toString();
			}
		});
		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(recipeCheckboxTableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_3 = tableViewerColumn_3.getColumn();
		tblclmnNewColumn_3.setWidth(80);
		tblclmnNewColumn_3.setText("Unit");
		//tableViewerColumn_3.setLabelProvider(new ComboBoxLabelProvider(new String[]{"ff","gg"}));
		tableViewerColumn_3.setLabelProvider(new ColumnLabelProvider(){
			@Override
			public String getText(Object element) {
				// TODO Auto-generated method stub
				Unit unit=((MealRecipe)element).getUnit();
				return unit==null?"":unit.getName();
			}
		});
		tableViewerColumn_3.setEditingSupport(new EditingSupport(recipeCheckboxTableViewer){
			private ComboBoxViewerCellEditor cellEditor;
			@Override
			protected CellEditor getCellEditor(Object element) {
				// TODO Auto-generated method stub
				cellEditor=new ComboBoxViewerCellEditor(recipeCheckboxTableViewer.getTable());
				cellEditor.setLabelProvider(new LabelProvider(){

					@Override
					public String getText(Object element) {
						// TODO Auto-generated method stub
						return ((Unit)element).getName();
					}
					
				});
		        cellEditor.setContentProvider(new ArrayContentProvider());
		        cellEditor.setInput(unitService.getUnitsByUnitName(null));
				return cellEditor;
			}

			@Override
			protected boolean canEdit(Object element) {
				// TODO Auto-generated method stub
				return true;
			}

			@Override
			protected Object getValue(Object element) {
				// TODO Auto-generated method stub
				Unit unit=((MealRecipe)element).getUnit();
				return unit;
			}

			@Override
			protected void setValue(Object element, Object value) {
				// TODO Auto-generated method stub
				Unit unit=(Unit)value;
				((MealRecipe)element).setUnit(unit);
				recipeCheckboxTableViewer.refresh();
			}
			
		});
		
		Group grpMealsPicturePreview = new Group(parent, SWT.NONE);
		grpMealsPicturePreview.setText("Meal's Preview");
		grpMealsPicturePreview.setLayout(new GridLayout(2, false));
		GridData gd_grpMealsPicturePreview = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_grpMealsPicturePreview.heightHint = 260;
		gd_grpMealsPicturePreview.widthHint = 426;
		grpMealsPicturePreview.setLayoutData(gd_grpMealsPicturePreview);
		
		canvasMealPicture = new Canvas(grpMealsPicturePreview, SWT.NONE);
		canvasMealPicture.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 3));
		canvasMealPicture.addPaintListener(new PaintListener(){
			public void paintControl(PaintEvent e) {
				if(image!=null){
					System.out.println("canvas width "+canvasMealPicture.getBounds().width+" , height"+canvasMealPicture.getBounds().height);
					System.out.println("image width "+image.getBounds().width+" , height"+image.getBounds().height);
					int imageWidth=image.getBounds().width;
					int imageHeight=image.getBounds().height;
				}
			}
		});
	}
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}
}